package cn.mju.tankfighterbackend.controller

import cn.mju.tankfighterbackend.annotation.security.IsPlayer
import cn.mju.tankfighterbackend.bean.AjaxResult
import cn.mju.tankfighterbackend.service.game.GameService
import cn.mju.tankfighterbackend.toAjaxResult
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.web.bind.annotation.*
import java.security.Principal

@RestController
@RequestMapping("game")
class GameController @Autowired constructor(
    val gameService: GameService
) {

    /**
     * 创建一个游戏 创建者会直接加入游戏
     */
    @IsPlayer
    @PostMapping("create")
    fun createGame(@RequestBody req: CreateGameReq, principal: Principal): AjaxResult<CreateGameResp> {
        gameService.createNewGame(req.playerCount).let { rest ->
            if (rest.isSuccess) {
                val pair = rest.getOrNull()
                val (gameId, houseCode) = pair!!
                val joinGame = gameService.joinGame(houseCode, principal.name)
                if (joinGame.isSuccess) {
                    return AjaxResult.success(CreateGameResp(gameId, houseCode))
                } else {
                    return AjaxResult.error(joinGame.exceptionOrNull()?.message)
                }

            } else {
                return AjaxResult.error(rest.exceptionOrNull()?.message)
            }

        }
    }


    @IsPlayer
    @PostMapping("join")
    fun joinGame(
        @RequestBody req: JoinGameReq,
        principal: Principal
    ): AjaxResult<JoinGameResp> {

        return gameService.joinGame(req.houseCode, principal.name).let { result ->
            if (result.isSuccess) {
                AjaxResult.success(gameService.getPlayerNamesInGame(result.getOrNull()!!)?.let { JoinGameResp(it) })
            } else {
                AjaxResult.error(result.exceptionOrNull()?.message)
            }
        }
    }

    @IsPlayer
    @GetMapping("playerInHouse")
    fun getPlayerInHouse(
        principal: Principal
    ): AjaxResult<PlayerInHouseResp> {

        var context = SecurityContextHolder.getContext()
        var authentication = context.authentication
        var principal1 = authentication.principal
        gameService.getGameIdByPlayer(principal.name)?.let { gameId ->
            gameService.getPlayerNamesInGame(gameId)?.let { playerNames ->
                return AjaxResult.success(PlayerInHouseResp(playerNames))
            }
        }
        return AjaxResult.error("未找到游戏!")
    }



    @IsPlayer
    @GetMapping("getGameId")
    fun getGameId(
        principal: Principal
    ): AjaxResult<Long> {

        val gameIdByPlayer = gameService.getGameIdByPlayer(principal.name)
        return if (gameIdByPlayer == null) {
            AjaxResult.error("您当前不在任何房间中!")
        } else {
            AjaxResult.success(gameIdByPlayer)
        }

    }

    @IsPlayer
    @PostMapping("exit")
    fun exitGame(
        @RequestBody req: JoinGameReq,
        @AuthenticationPrincipal principal: Principal
    ): AjaxResult<JoinGameResp> {
        return gameService.exitGame(principal.name).toAjaxResult()
    }
}

data class CreateGameReq(
    val playerCount: Int
)

data class CreateGameResp(
    val gameId: Long,
    val houseCode: Int
)

data class JoinGameReq(
    val houseCode: Int
)

data class JoinGameResp(
    val players: List<String>
)



data class PlayerInHouseResp(
    val players: List<String>
)